package com.yidian.consumer;

import com.alibaba.fastjson.JSON;
import com.yidian.pojo.order.OrderItem;
import com.yidian.server.goods.StockBackService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author whb
 * @create 2021-09-06 10:54
 * @desciption 消息消费者监听类
 */
public class BackMessageConsumer implements MessageListener {
    @Autowired
    private StockBackService stockBackService;

    /**
     * 接收库存回滚消息，并存入到库存回滚表
     * @param message 回滚消息
     */
    @Override
    public void onMessage(Message message) {
        try {
            String stockMsg=new String(message.getBody());//获取rabbitmq生产者发送的消息
            List<OrderItem> orderItemList = JSON.parseArray(stockMsg, OrderItem.class);//将json字符串转化成对应类型的集合
            stockBackService.addStockBackList(orderItemList);//库存回滚信息存入回滚表
        } catch (Exception e) {
            e.printStackTrace();
            //库存回滚异常存入日志，进行人工干预
        }
    }
}
